package com.lzb.newloan;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzb.newloan.utils.BaseActivity;
import com.lzb.newloan.utils.Code;
import com.lzb.newloan.utils.Config;
import com.lzb.newloan.utils.EditUtils;
import com.lzb.newloan.utils.HttpUtils;
import com.lzb.newloan.utils.TimeCount;

import org.json.JSONException;
import org.json.JSONObject;

public class RegisterActivity extends BaseActivity implements OnClickListener {
	private EditText phone, pwd, code, adv_phone;
	private Button codeBtn;

	private EditText et_phoneCode;
	private ImageView iv_showCode;
	private String realCode;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		initView();
	}

	private void initView() {
		phone = (EditText) findViewById(R.id.phone);

		code = (EditText) findViewById(R.id.code);

		TextView title_txt_center = (TextView) findViewById(R.id.title_txt_center);
		title_txt_center.setText("注册");
		findViewById(R.id.register_next_btn).setOnClickListener(this);
		codeBtn = (Button) findViewById(R.id.codeBtn);
		codeBtn.setOnClickListener(this);

		RelativeLayout backpress = (RelativeLayout) findViewById(R.id.backpress);
		backpress.setOnClickListener(this);


		et_phoneCode = (EditText) findViewById(R.id.et_phoneCodes);
		iv_showCode = (ImageView) findViewById(R.id.iv_showCode);
		//将验证码用图片的形式显示出来
		iv_showCode.setImageBitmap(Code.getInstance().createBitmap());
		realCode = Code.getInstance().getCode().toLowerCase();
		iv_showCode.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.iv_showCode:
				iv_showCode.setImageBitmap(Code.getInstance().createBitmap());
				realCode = Code.getInstance().getCode().toLowerCase();
				Log.v("TAG", "realCode" + realCode);
				break;

		case R.id.codeBtn:// 验证码
			String phoneCode = et_phoneCode.getText().toString().toLowerCase();
			String phoneStr = phone.getText().toString();
			if (TextUtils.isEmpty(phoneStr)) {
			Toast.makeText(RegisterActivity.this, "手机号码不能为空", Toast.LENGTH_SHORT).show();
			return;
		}

			/** 去除手机号码中的空格 */
			if (phoneStr.contains(" ")) {
				phoneStr = phoneStr.replaceAll(" ", "");
			}

			/** 检查手机号码格式是否正确 */
			if (!EditUtils.isMobileNO(phoneStr)) {
				Toast.makeText(RegisterActivity.this, "手机号码格式不正确", Toast.LENGTH_SHORT).show();
				return;
			}

			if (phoneCode.contains(" ")) {
				Toast.makeText(RegisterActivity.this, "图形校验码不能为空", Toast.LENGTH_SHORT).show();
				return;
			}

			if (phoneCode.equals(realCode)) {
				ConnectionDetector cd = new ConnectionDetector(RegisterActivity.this);
				if (cd.isConnectingToInternet()) {
					String url = Config.REGISTER_SEND_CODE + "&mobile=" + phoneStr
							+ "&type=0";
					HttpUtils.doPostAsyn(url, "",mHandler, Config.CODE_SEND);
				}

			} else {
				iv_showCode.setImageBitmap(Code.getInstance().createBitmap());
				realCode = Code.getInstance().getCode().toLowerCase();
				Toast.makeText(RegisterActivity.this, "图形校验码不正确", Toast.LENGTH_SHORT).show();
				et_phoneCode.setText("");
				return;
			}

			break;

			case R.id.register_next_btn:
				String phoneCode2 = et_phoneCode.getText().toString().toLowerCase();
				String phoneStr2 = phone.getText().toString();
				if (TextUtils.isEmpty(phoneStr2)) {
					Toast.makeText(RegisterActivity.this, "手机号码不能为空", Toast.LENGTH_SHORT).show();
					return;
				}

				/** 去除手机号码中的空格 */
				if (phoneStr2.contains(" ")) {
					phoneStr2 = phoneStr2.replaceAll(" ", "");
				}

				/** 检查手机号码格式是否正确 */
				if (!EditUtils.isMobileNO(phoneStr2)) {
					Toast.makeText(RegisterActivity.this, "手机号码格式不正确", Toast.LENGTH_SHORT).show();
					return;
				}

				if (TextUtils.isEmpty(phoneCode2)) {
					Toast.makeText(RegisterActivity.this, "图形校验码不能为空", Toast.LENGTH_SHORT).show();
					return;
				}
				if (TextUtils.isEmpty(code.getText().toString())) {
					Toast.makeText(RegisterActivity.this, "短信校验码不能为空", Toast.LENGTH_SHORT).show();
					return;
				}

				Intent intent = new Intent(RegisterActivity.this, Register2Activity.class);
				intent.putExtra("code",code.getText().toString());
				intent.putExtra("phone",phone.getText().toString());
				startActivity(intent);
				break;
		case R.id.backpress:
			finish();
			break;
		default:
			break;
		}
	}


	private Handler mHandler = new Handler() {

		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case Config.CODE_SEND:
				String reString = msg.obj.toString();
				try {
					JSONObject jsonObject = new JSONObject(reString);
					int error = jsonObject.optInt("error");

					if (error > 0) {
						Toast.makeText(RegisterActivity.this,
								jsonObject.getString("msg"), Toast.LENGTH_SHORT)
								.show();
//						timeC.cancel();
					}else{
						timeC = new TimeCount(180000, 1000, codeBtn, "重新获取");
						timeC.start();
					}

				} catch (JSONException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				break;
			case Config.CODE_URL_ERROR:
				Toast.makeText(RegisterActivity.this, "url错误",
						Toast.LENGTH_SHORT).show();
				break;
			case Config.CODE_NET_ERROR:
				Toast.makeText(RegisterActivity.this, "网络错误",
						Toast.LENGTH_SHORT).show();
				break;

			default:
				break;
			}
		};
	};
	private TimeCount timeC;


	@Override
	public void processMessage(Message message) {
		// TODO Auto-generated method stub
		
	}

}
